package me.zhengjie.spider.util.processor;

import cn.hutool.core.util.StrUtil;
import lombok.Data;
import me.zhengjie.spider.common.SpiderConstant;
import me.zhengjie.spider.domain.video.VideoBean;
import me.zhengjie.spider.domain.video.VideoRuleBean;
import me.zhengjie.utils.StringUtils;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.processor.PageProcessor;

/**
 * 爬取视频页的大概信息
 */
@Data
@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class SpiderVideoGeneralPageProcessor implements PageProcessor {

    private VideoBean bean;
    private VideoRuleBean ruleBean;

    @Override
    public void process(Page page) {
        page.putField(SpiderConstant.IMG, page.getHtml().xpath(this.ruleBean.getImg()).regex(SpiderConstant.JPG_REGEX).all());
        page.putField(SpiderConstant.HREF, page.getHtml().xpath(this.ruleBean.getHref()).all());
        page.putField(SpiderConstant.ALT, page.getHtml().xpath(this.ruleBean.getAlt()).all());
        if (null == this.ruleBean.getTargetType() || StringUtils.equalsIgnoreCase(this.ruleBean.getTargetType(), SpiderConstant.TARGET_TYPE_REGEX)) {
            page.addTargetRequests(page.getHtml().links().regex(this.ruleBean.getTargetLink()).all());
        } else if (StringUtils.equalsIgnoreCase(this.ruleBean.getTargetType(), SpiderConstant.TARGET_TYPE_XPATH)) {
            page.addTargetRequests(page.getHtml().xpath(this.ruleBean.getTargetLink()).all());
        }
    }
}
